package cn.bj.yu.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 周宇
 * @create 2020-08-13 10:28
 * 1、探究原理
 * 2、觉得自己会用
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //new Thread(new Runnable()).start();
        //new Thread(new FutureTask<V>(Callable())).start();

        MyCallThread callThread = new MyCallThread();
        FutureTask<Integer> futureTask = new FutureTask<>(callThread);//适配类

        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();//結果會被緩存 效率高<之打印一行call方法...>

        //获取Callable的返回结果
        Integer integer = futureTask.get();  //这个get方法可能会产生阻塞！ 把他放到最后一行、
        //或者使用异步通信来处理!
        System.out.println(integer);
    }
}

class MyCallThread implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("执行call方法...");
        //耗时的操作
        return 1024;
    }
}
